package controller;

import po.User;
import service.UserService;
import service.impl.UserServiceImpl;
import util.Constant;
import util.SendMsgUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Random;

@WebServlet("/register")
public class RegesitServlet extends HttpServlet {

    // 重写doGet方法进行页面验证码和手机号码的验证
    // 进行短信验证码的验证工作

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String action = req.getParameter("action");
        switch (action){
            case "shortmsgcheck":  this.shortMsgCheck(req,resp);    break;
            case "pageMsgcheck": this.pageMsgCheck(req,resp);   break;
        }





    }
// 短信验证码的验证工作
    private void shortMsgCheck(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取用户提交的验证ma
        // 获取session内部的短信验证码 进行比对

        String shortMsg = req.getParameter("shortmsg");
        HttpSession session = req.getSession();
        String oriMsg = (String) session.getAttribute("shortMsg");

        // 可能会报空指针
        // TODO

        String result;
        if(shortMsg.equals(oriMsg)){
            // 去验证之前是否已经注册过，如果已经注册过，页面要切换到登陆页面。
            // 如果没有注册过，页面要切换到输入密码部分。
            UserService service = new UserServiceImpl();
            boolean checkRs=false ;
            try {
                String phone =(String) session.getAttribute("registePhone");
                checkRs= service.checkResgist(phone);
            } catch (Exception e) {
                e.printStackTrace();
            }
            if(checkRs == Constant.PHONE_ALREADY_REGISTE){
                // 当手机已经注册过
                result=Constant.PHONE_ALREADY_REGISTE_PAGE_INFO;
            }else{
                result=Constant.SHORT_MSG_SUCCESS;
            }



        }else{
            result = Constant.SHORT_MSG_ERROR;
        }
        resp.getWriter().write(result);
    }

    private void pageMsgCheck(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        // 获取页面的msg 获取页面的手机号

        String msg = req.getParameter("msg");
        String phone = req.getParameter("phone");
        // 判断用户输入的验证码是否正确，
        // 1 获取session
        HttpSession session = req.getSession(true);
        session.setAttribute("registePhone",phone);

        // 2 从session获取原验证码的值
        String origMsg = (String)session.getAttribute("KAPTCHA_SESSION_KEY");
        // 3 和页面验证码进行比对
        if(msg.equals(origMsg)){
            this.sendMsg(phone,session);
            // TODO 调用第三方平台发送短信
            resp.getWriter().write(Constant.PAGE_MSG_SUCCESS);
        }else{
            // 如果不正确，页面提示验证码失败，请重写输入
            resp.getWriter().write(Constant.PAGE_MSG_ERREOR);
        }


    }



    private void sendMsg(String phone,HttpSession session){
        // 产生一个随机码 4位随机数字
        StringBuffer stringBuffer = new StringBuffer();
        Random random = new Random();
        for(int i= 1;i<=4;i++) {
            stringBuffer.append(random.nextInt(10));
        }
         String msgCode = stringBuffer.toString();
        // 保存到session
        session.setAttribute("shortMsg",msgCode);
        System.out.println("短信验证码是："+msgCode);
        // 调用工具类进行发送短信
    //   SendMsgUtil.sendMsg(phone,msgCode);

    }


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action = req.getParameter("action");
        switch (action){
            case "registeUser":  registeUser(req,resp);   break;
        }
    }

    private void registeUser(HttpServletRequest req, HttpServletResponse resp) {
        // 获取用户提交的密码，获取用户的手机号 将信息去处理业务逻辑层去处理
        String pwd = req.getParameter("pwd");
        String phone = (String) req.getSession().getAttribute("registePhone");
         User  user = new User(phone,pwd);

         UserService service  = new UserServiceImpl();
         boolean flag=false;
        try {
            flag= service.addUser(user);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            // 1表示保存成功，0表示保存失败
            resp.getWriter().write(flag?'1':'0');
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
